const db = require('../db');

const UserData = require('./data/user.json');
const CategoryData = require('./data/category.json');
const PostData = require('./data/post.json');
const LikeData = require('./data/like.json');
const CommentData = require('./data/comment.json');
const FollowData = require('./data/follow.json');
const CoverData = require('./data/cover.json');

db.sequelize.sync({
    force: true
}).then(async () => {
    try {
        const {
            User,
            Category,
            Comment,
            Post,
            Like,
            Follow,
            Cover
        } = db;

        await User.bulkCreate(UserData);
        await Category.bulkCreate(CategoryData);
        do {
            let temp = PostData.shift();
            let {
                categoryId
            } = temp;
            delete temp.categoryId;

            let post = await Post.create(temp);
            let category = await Category.findAll({
                where: {
                    id: categoryId
                }
            });
            post.setCategories(category);
        }
        while (PostData.length);

        await Comment.bulkCreate(CommentData);

        do {
            let temp = LikeData.splice(0, 20);
            await Like.bulkCreate(temp);
        } while (LikeData.length)

        do {
            let temp = FollowData.splice(0, 20);
            await Follow.bulkCreate(temp);
        } while (FollowData.length);

        await Cover.bulkCreate(CoverData);

        console.log('数据准备完毕');
    } catch (err) {
        console.log(err);
    }
})